/*
 * 
 *
 *
 */
package com.hboxs.asl.qcloudLVB.dao.impl;

import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.impl.BaseDaoImpl;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.qcloudLVB.dao.LiveDao;
import com.hboxs.asl.qcloudLVB.entity.Live;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Dao - 直播记录
 */
@Repository("liveDaoImpl")
public class LiveDaoImpl extends BaseDaoImpl<Live, Long> implements LiveDao {

    @Override
    public Live findByAnchorAndStatus(Member anchor, Live.LiveStatus liveStatus) {
        if (anchor == null) {
            return null;
        }
        try {
            String jpql = "select lives from Live lives where lower(lives.anchor) = lower(:anchor) and lives.liveStatus = :liveStatus";
            return entityManager.createQuery(jpql, Live.class).setFlushMode(FlushModeType.COMMIT).setParameter("anchor", anchor).setParameter("liveStatus", liveStatus).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    @Override
    public Page<Live> findPage(Pageable pageable, Live.LiveStatus liveStatus, Live.Type type) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Live> criteriaQuery = criteriaBuilder.createQuery(Live.class);
        Root<Live> root = criteriaQuery.from(Live.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type));
        if (liveStatus != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("liveStatus"), liveStatus));
            criteriaQuery.where(criteriaBuilder.equal(root.get("liveStatus"), liveStatus));
        }
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate")));
        return super.findPage(criteriaQuery, pageable);
    }

    @Override
    public Live offLive(Member anchor) {
        if (anchor == null) {
            return null;
        }
        try {
            String jpql = "select lives from Live lives where lower(lives.anchor) = lower(:anchor) and (lives.liveStatus = :liveStatus1 or lives.liveStatus = :liveStatus2)";
            return entityManager.createQuery(jpql, Live.class).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("anchor", anchor).setParameter("liveStatus1", Live.LiveStatus.nolive)
                    .setParameter("liveStatus2", Live.LiveStatus.live).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    @Override
    public List<Live> findConnectLive(Long connectLiveId) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Live> criteriaQuery = criteriaBuilder.createQuery(Live.class);
        Root<Live> root = criteriaQuery.from(Live.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("connectLiveId"), connectLiveId));
        criteriaQuery.where(restrictions);
        return super.findList(criteriaQuery, null,null,null,null);
    }

    @Override
    public List<Live> findConnectLive(Long connectLiveId, Live.LiveStatus liveStatus) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Live> criteriaQuery = criteriaBuilder.createQuery(Live.class);
        Root<Live> root = criteriaQuery.from(Live.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("connectLiveId"), connectLiveId));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("liveStatus"), liveStatus));
        criteriaQuery.where(restrictions);
        return super.findList(criteriaQuery, null,null,null,null);
    }

    @Override
    public long getHostLiveCounts(Live.Type type) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Live> criteriaQuery = criteriaBuilder.createQuery(Live.class);
        Root<Live> root = criteriaQuery.from(Live.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type));
        criteriaQuery.where(restrictions);
        return super.count(criteriaQuery, null);
    }
}
